package cn.controller;

import cn.pojo.Backend_user;
import cn.pojo.Dev_user;
import cn.service.backend_user.Backend_userService;
import cn.tools.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RequestMapping("/backend")
@Controller
public class Backend_userController {
    @Resource
    private Backend_userService backend_userService;

    @RequestMapping("/from")
    public String fromlogin(HttpServletRequest request){
        request.setAttribute("who","backend");
        return "forward:/login.jsp";
    }

    //后台登录
    @RequestMapping("/login")
    public String dev_userLogin(String userCode, String userPassword, HttpServletRequest request, HttpSession session){
            Backend_user user = backend_userService.getBackend_userLogin(userCode, userPassword);
            if (user != null) {
                //放入session
                request.getSession().setAttribute(Constants.USER_SESSION,user);
                System.out.println( ((Backend_user)request.getSession().getAttribute(Constants.USER_SESSION)).getId());
                request.getSession().setAttribute("user",((Backend_user)request.getSession().getAttribute(Constants.USER_SESSION)).getUserCode());
                return "user_list";
            } else {
                request.setAttribute("msg", "账号名或密码错误!");
                return "forward:/backend/from";
            }
    }
}
